package com.kyxx.mycommonproject.gouwuche;

import android.app.Dialog;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.kyxx.mycommonproject.R;
import com.kyxx.mycommonproject.base.BaseActivity0;
import com.kyxx.mycommonproject.net.ResponseListener;
import com.kyxx.mycommonproject.net.RestClient;
import com.kyxx.mycommonproject.net.UrlUtils;
import com.kyxx.mycommonproject.utils.BigDecimalUtils;
import com.kyxx.mycommonproject.utils.UILUtils;
import com.kyxx.mycommonproject.widget.ListViewForScrollView;
import com.loopj.android.http.RequestParams;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import cz.msebera.android.httpclient.Header;

public class SubmitOrderActivity extends BaseActivity0 {
    List<CartBean.GoodsInfoBean> goodsInfoBeanList = new ArrayList<>();
    private RelativeLayout rl_address;
    private TextView tv_name;
    private TextView tv_phone;
    private TextView tv_address;
    private EditText et_liuyan;
    private TextView tv_price;
    private TextView tv_heji;
    private ListViewForScrollView lv_order;
    private LinearLayout btn_pay;
    private GoodsAdapter goodsAdapter;
    private int goodsNum;
    private double goodPrice;

    @Override
    protected void loadViewLayout() {
        setContentView(R.layout.activity_submit_order);
    }

    @Override
    protected void findViewById() {
        rl_address = (RelativeLayout) findViewById(R.id.rl_address);
        tv_name = (TextView) findViewById(R.id.tv_name);
        tv_phone = (TextView) findViewById(R.id.tv_phone);
        tv_address = (TextView) findViewById(R.id.tv_address);
        et_liuyan = (EditText) findViewById(R.id.et_liuyan);
        tv_heji = (TextView) findViewById(R.id.tv_heji);
        lv_order = (ListViewForScrollView) findViewById(R.id.lv_order);
        tv_price = (TextView) findViewById(R.id.tv_price);
        lv_order.setFocusable(false);
        btn_pay = (LinearLayout) findViewById(R.id.btn_pay);
    }

    @Override
    protected void setListener() {
        btn_pay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
            }
        });
    }

    @Override
    protected void processLogic() {
        loadToolBar("提交订单");
        goodsInfoBeanList = (ArrayList<CartBean.GoodsInfoBean>) getIntent().getSerializableExtra("submitgoods");
        refreshMoney();
        initReceiveData();
        goodsAdapter = new GoodsAdapter();
        lv_order.setAdapter(goodsAdapter);
    }

    class GoodsAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return goodsInfoBeanList.size();
        }

        @Override
        public Object getItem(int i) {
            return goodsInfoBeanList.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            final ViewHolder0 holder;
            if (view == null) {
                holder = new ViewHolder0();
                view = View.inflate(SubmitOrderActivity.this, R.layout.item_submit_order, null);
                holder.img_goods = (ImageView) view.findViewById(R.id.img_goods);
                holder.tv_goods_name = (TextView) view.findViewById(R.id.tv_goods_name);
                holder.tv_goods_price = (TextView) view.findViewById(R.id.tv_goods_price);
                holder.tv_goods_num = (TextView) view.findViewById(R.id.tv_goods_num);
                view.setTag(holder);
            } else {
                holder = (ViewHolder0) view.getTag();
            }
            UILUtils.displayImage(SubmitOrderActivity.this, goodsInfoBeanList.get(i).getGoods_image(), holder.img_goods);
            holder.tv_goods_name.setText(goodsInfoBeanList.get(i).getGoods_name());
            holder.tv_goods_price.setText("¥" + goodsInfoBeanList.get(i).getGoods_price());
            holder.tv_goods_num.setText("数量×" + goodsInfoBeanList.get(i).getGoods_num());
            return view;
        }
    }

    class ViewHolder0 {
        ImageView img_goods;
        TextView tv_goods_name;
        TextView tv_goods_price;
        TextView tv_goods_num;
    }

    private void refreshMoney() {
        goodsNum = 0;
        goodPrice = 0;
        for (int i = 0; i < goodsInfoBeanList.size(); i++) {
            goodsNum = goodsNum + Integer.valueOf(goodsInfoBeanList.get(i).getGoods_num());
            goodPrice = goodPrice + Double.valueOf(BigDecimalUtils.mul(Integer.valueOf(goodsInfoBeanList.get(i).getGoods_num()), Double.valueOf(goodsInfoBeanList.get(i).getGoods_price())));
        }
        tv_heji.setText(String.format(Html.fromHtml("共%d件商品 合计¥<font color=red>%2$2.2f</font>元").toString(), goodsNum, goodPrice));
//        tv_heji.setText(Html.fromHtml("共%d件商品 合计¥<font color=red>%2$2.2f</font>元"));
        tv_price.setText(String.format("¥%1$2.2f", goodPrice));
    }

    private void initReceiveData() {
        RequestParams params = new RequestParams();
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("token", "111111");
            params.put("json", jsonObject.toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RestClient.post(UrlUtils.GETUSERINFO, params, SubmitOrderActivity.this, new ResponseListener(SubmitOrderActivity.this, true) {
            @Override
            public void success(int arg0, Header[] arg1, JSONObject result) {
                try {
                    tv_name.setText(result.getJSONObject("data").getString("nickname"));
                    tv_phone.setText(result.getJSONObject("data").getString("mobile"));
                    tv_address.setText(result.getJSONObject("data").getString("shopaddr"));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }
}
